package com.joysuch.wwyt.attendance.controller;


import com.google.common.collect.Lists;
import com.joysuch.wwyt.attendance.bean.AttendanceUserPageBean;
import com.joysuch.wwyt.attendance.bean.AttendanceUserParam;
import com.joysuch.wwyt.attendance.bean.UserTaskBean;
import com.joysuch.wwyt.attendance.dto.AttendanceUserDto;
import com.joysuch.wwyt.attendance.excel.AttendanceUserExcelService;
import com.joysuch.wwyt.attendance.service.AttendanceApiUserService;
import com.joysuch.wwyt.attendance.service.AttendanceUserService;
import com.joysuch.wwyt.bp.entity.bean.TaskForAssignBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zqw
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/attendance/user")
@Slf4j
@Api(tags = "人员排班")
public class AttendanceUserController {

    @Autowired
    private AttendanceUserService userService;
    @Autowired
    private AttendanceUserExcelService excelService;
    @Autowired
    private AttendanceApiUserService apiUserService;

    @PostMapping("saveUserTask")
    @ApiOperation("保存人员考勤班次")
    @RequiresPermissions("attendance:user:save")
    public ResultBean saveUserTask(@RequestBody AttendanceUserDto dto) {
        try {
            userService.saveUserTask(dto);
            return ResultBean.defaultSuccessResult();
        } catch (ApiBusinessException e) {
            return ResultBean.wrapErrorResult(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("saveFixTask")
    @ApiOperation("手动生成固定排班任务")
    public ResultBean saveFixTask(Integer year, Integer month) {
        userService.saveFixTask(year, month);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("pageUserTask")
    @ApiOperation("人员排班分页")
    public ResultBean pageUserTask(@RequestBody AttendanceUserParam pageParam) {
        AttendanceUserPageBean pageBean = userService.pageUserTask(pageParam);
        return ResultBean.success(pageBean);
    }

    @PostMapping("importUserExcel")
    @ApiOperation("人员排班导入")
    @RequiresPermissions("attendance:user:importUserExcel")
    public ResultBean importUserExcel(MultipartFile file, Integer year, Integer month) {
        List<String> errorMessage = null;
        try {
            if (!file.getOriginalFilename().endsWith("xlsx") && !file.getOriginalFilename().endsWith("xls")) {
                return ResultBean.success(Lists.newArrayList("文件类型不正确"));
            }
            errorMessage = excelService.importUserExcel(file, year, month);
            return ResultBean.success(errorMessage);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.success(Lists.newArrayList("导入失败:" + e.getMessage()));
        }
    }

    @GetMapping("exportUserExcel")
    @ApiOperation("人员排班导出")
 //   @RequiresPermissions("attendance:user:exportUserExcel")
    public ResultBean exportUserExcel(HttpServletResponse response, Integer templateFlag, Integer year, Integer month, Long groupId) {
        try {
            SXSSFWorkbook workBook = excelService.exportUserExcel(templateFlag, year, month, groupId);
            ServletOutputStream outputStream = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(year + "年" + month + "月人员排班.xlsx", "UTF-8"));
            workBook.write(outputStream);
            outputStream.close();
            workBook.close();
            return ResultBean.success("下载成功...... ");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.fail(101, "下载失败:" + e.getMessage());
        }
    }

    @PostMapping("userTaskCheck")
    @ApiOperation("校验人员是否有排班")
    public ResultBean userTaskCheck(@RequestBody UserTaskBean bean) {
        return ResultBean.success(apiUserService.userTaskCheck(bean.getStartDate(), bean.getUserIds()));
    }

    @PostMapping("batchUserTaskCheck")
    @ApiOperation("校验人员是否有排班")
    public ResultBean batchUserTaskCheck(@RequestBody List< TaskForAssignBean > beanList) {
        return ResultBean.success(apiUserService.batchUserTaskCheck(beanList));
    }


}

